﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace XthTicketDesktop
{

    public class ApiService
    {
        private static readonly HttpClient client = new HttpClient();

        // HTTP GET 请求
        public static async Task<string> GetAsync(string url)
        {
            try
            {
                HttpResponseMessage response = await client.GetAsync(url);
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                return responseBody;
            }
            catch (HttpRequestException e)
            {
                MessageBox.Show($"请求错误: {e.Message}");
                return null;
            }
        }

        // HTTP POST 请求
        public static async Task<string> PostAsync(string url, HttpContent content)
        {
            try
            {
                HttpResponseMessage response = await client.PostAsync(url, content);
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                return responseBody;
            }
            catch (HttpRequestException e)
            {
                MessageBox.Show($"请求错误: {e.Message}");
                return null;
            }
        }
    }
}
